/**
 * SrvUtilityPortlets -  http://code.google.com/p/alui-toolbox/
 * Copyright (c) 2008-2009
 * by Fabien Sanglier
 * 
 * This file is part of SrvUtilityPortlets.
 * 
 * SrvUtilityPortlets is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * SrvUtilityPortlets is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with SrvUtilityPortlets.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AluiServerLib.Helper;
using com.plumtree.server;
using AluiServerLib.Models;
using AluiServerLib.Business;

namespace ALUIServerUtilities
{
    public partial class PageLinkListing : System.Web.UI.Page
    {
        public const string SETTINGNAME_PAGEOPENER = "PageLinkListing.pageopener";
        public const string SETTINGNAME_DEFAULTHOSTOPENER = "PageLinkListing.defaultopenerhost";
        public const string SETTINGNAME_IMPERSONATEGUEST = "PageLinkListing.ImpersonateGuest";
        public const string SETTINGNAME_USERID = "PageLinkListing.UserID";
        public const string SETTINGNAME_PASSWORD = "PageLinkListing.Password";

        private int m_topfolderid = 0;
        private bool m_includesubfolders = false;
        private string m_hostpageopener = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request["topfolderid"]))
                {
                    try
                    {
                        m_topfolderid = int.Parse(Request["topfolderid"]);
                    }
                    catch (Exception exc)
                    {
                        m_topfolderid = 0;
                    }
                }

                if (!string.IsNullOrEmpty(Request["includesubfolders"]))
                {
                    try
                    {
                        m_includesubfolders = bool.Parse(Request["includesubfolders"]);
                    }
                    catch (Exception exc)
                    {
                        m_includesubfolders = false;
                    }
                }


                if (!string.IsNullOrEmpty(Request["openerhost"]))
                {
                    m_hostpageopener = Request["openerhost"]; 
                } else {
                    m_hostpageopener =  ConfigurationManager.AppSettings[SETTINGNAME_DEFAULTHOSTOPENER];
                }

                //create a user session
                IPTSession ptsession = null;

                //impersonnate guest user
                bool impersonateguest = false;
                try
                {
                    impersonateguest = bool.Parse(ConfigurationManager.AppSettings[SETTINGNAME_IMPERSONATEGUEST]);
                }
                catch (Exception exc)
                {
                    impersonateguest = false;
                }        
                
                if (impersonateguest)
                {
                    ptsession = NativeApplicationMgr.GetInstance().GetGuestSession();
                }
                else
                {
                    ptsession = NativeApplicationMgr.GetInstance().GetNativeSession(
                    ConfigurationManager.AppSettings[SETTINGNAME_USERID],
                    ConfigurationManager.AppSettings[SETTINGNAME_PASSWORD],
                    true,
                    null);
                }

                if (null != ptsession)
                {
                    PTNavigationModel ptnav = new PTNavigationModel(ptsession);
                    ArrayList arrPages = null;
                    if (m_topfolderid > 0)
                    {
                        arrPages = ptnav.GetAvailablePages(m_topfolderid, m_includesubfolders);
                    }
                    else
                    {
                        arrPages = ptnav.GetAvailablePages(m_includesubfolders);
                    }

                    if (null != arrPages && arrPages.Count > 0)
                    {
                        rptPages.DataSource = arrPages;
                        rptPages.DataBind();
                    }
                }
            }
        }

        protected void rptPages_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ALUIPage page = (ALUIPage)e.Item.DataItem;

                Literal lnkPortalPage = (Literal)e.Item.FindControl("lnkPortalPage");
                if (null != lnkPortalPage)
                {

                    lnkPortalPage.Text = "<a href=\"" +
                        String.Format(ConfigurationManager.AppSettings[SETTINGNAME_PAGEOPENER], m_hostpageopener, page.GetObjectID()).Replace("&nbsp;", "&") +
                        "\">" +
                        page.GetName() +
                        "</a>";
                }
            }
        }
    }
}
